package org.vdab.projects.tasks.server.services;

import java.util.LinkedList;
import java.util.List;

import org.vdab.projects.tasks.client.TasksService;
import org.vdab.projects.tasks.client.util.MessageGuard;
import org.vdab.projects.tasks.server.dal.ServerEntityManager;
import org.vdab.projects.tasks.shared.EntityManager;
import org.vdab.projects.tasks.shared.entities.ITaskBook;
import org.vdab.projects.tasks.shared.entities.impl.Task;
import org.vdab.projects.tasks.shared.entities.impl.TaskBook;
import org.vdab.projects.tasks.shared.entities.impl.TaskList;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

/**
 * The server side implementation of the RPC service.
 * 
 * This class uses a 
 */
@SuppressWarnings("serial")
public class TasksServiceImpl extends RemoteServiceServlet implements
		TasksService {

	private ServerEntityManager em;
	
	public TasksServiceImpl() throws Exception{
		this.em = (ServerEntityManager)ServerEntityManager.get();
	}
		
	@Override
	public List<TaskBook> getTaskBook() throws Exception {
		List<TaskBook> result = new LinkedList<TaskBook>();
		for(ITaskBook tb : em.getTaskBooks()){
			result.add((TaskBook)tb);
		}		
		return result;
	}

	@Override
	public void saveTaskBook(TaskBook tb) throws Exception{
		MessageGuard.Log("In TasksServiceImpl.saveTaskBook()");
		em.saveTaskBook(tb);
	}
			
	@Override
	public void saveTaskList(TaskList tl) throws Exception {
		em.saveTaskList(tl);
	}

	@Override
	public void saveTask(Task task) throws Exception {
		em.saveTask(task);
	}
	
	/**
	 * Escape an html string. Escaping data received from the client helps to
	 * prevent cross-site script vulnerabilities.
	 * 
	 * @param html the html string to escape
	 * @return the escaped string
	 */
	@SuppressWarnings("unused")
	private String escapeHtml(String html) {
		if (html == null) {
			return null;
		}
		return html.replaceAll("&", "&amp;").replaceAll("<", "&lt;")
				.replaceAll(">", "&gt;");
	}

	@Override
	public TaskList getTaskListByKey(String key) throws Exception {
		return em.getTaskListByKey(key);
	}




}
